<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input clearable @clear="getGoodsList" placeholder="请输入内容" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="redirectAddGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 主体内容区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="95px"></el-table-column>
        <el-table-column label="商品数量" prop="goods_number" width="70px"></el-table-column>
        <el-table-column label="状态" prop="goods_state" width="50px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-tooltip :enterable="false" class="item" effect="dark" content="已审核" placement="top" v-if="scope.row.goods_state === 2">
              <el-button circle type="success" icon="el-icon-check" size="mini"></el-button>
            </el-tooltip>
            <el-tooltip :enterable="false" class="item" effect="dark" content="审核中" placement="top" v-else-if="scope.row.goods_state === 1">
              <el-button circle type="info" icon="el-icon-loading" size="mini"></el-button>
            </el-tooltip>
            <el-tooltip :enterable="false" class="item" effect="dark" content="未通过" placement="top" v-else-if="scope.row.goods_state === 0">
              <el-button circle type="danger" icon="el-icon-close" size="mini"></el-button>
            </el-tooltip>
            <el-tooltip :enterable="false" class="item" effect="dark" content="无状态" placement="top" v-else>
              <el-button circle type="warning" icon="el-icon-minus" size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" width="140px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <!-- 日期过滤器格式化时间戳 -->
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteGoods(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 20, 30]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 查询参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表数组对象
      goodsList: [],
      // 总数据条数
      total: 0
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 根据分页获取所有商品列表
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      // this.$message.success(res.meta.msg)
      // console.log(res.data);
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange (newPagesize) {
      this.queryInfo.pagesize = newPagesize
      this.getGoodsList()
    },
    handleCurrentChange (pageCurrent) {
      this.queryInfo.pagenum = pageCurrent
      this.getGoodsList()
    },
    // 通过商品id删除商品信息
    async deleteGoods (goodsId) {
      // 确认消息提示框
      const confirmRes = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes !== 'confirm') {
        return this.$message.info('取消了删除操作')
      }
      // 确认删除, 发起请求
      const { data: res } = await this.$http.delete(`goods/${goodsId}`)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getGoodsList()
    },
    // 跳转添加商品页面
    redirectAddGoodsPage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style lang="less" scoped>
</style>
